<?php 
namespace app\common\model;
class OrderConvert extends Common{
	protected $insert = ['addtime'=>SYS_TIME];
	public function pay_fail($ordersn, $userid=0, $price, $description){
		$info = $this->where('ordersn', $ordersn)->find();
		if ($info['status'] == 2) return false;
		if (!isset($userid) || !$userid) {
			$userid = $info['userid'];
		}
		$uuid = db("Member")->where("id",$userid)->value("uuid");
		if($uuid != 0){
			$url = setAppUrls(url('index/member/tixianjilu'),[],'blank');
			jpush([$uuid],'提现失败',$url,"3");
		}
		model("Member")->where('id', $userid)->dec('dis_amount', $price)->inc('amount', $price)->update();
        model("MemberAmountLog")->where('ordersn', $ordersn)->update(['status'=>2, 'description'=>$description]);
		return $this->where('userid', $userid)->where('ordersn', $ordersn)->update(['status'=>2, 'paytime'=>SYS_TIME]);
	}
	protected function getSysManageAttr($value, $data){
		if ($data['status'] != 0) {
			return '';
		}
		return "<a class='cocoAjaxPost' href='Javascript:' confirm='确定要审核通过吗？' ajax-url=".url('tixian')." ajax-data='{  id:".$data['id'].",status:1}'>审核通过</a> | <a class='cocoModal' href=".url("adddesc", ['id'=>$data['id'], 'status'=>2])." isForm  fixat='1' layerwidth='500px' layerheight='380px'>拒绝提现</a>";
	}
	protected function getRealnameAttr($value, $data){
		return model('AdminUser')->where('userid', $data['userid'])->value('realname');
	}
	protected function getStatusnameAttr($value, $data){
		$arr = [-1=>'兑换失败', 0=>'未付款', 1=>'等待发货', 2=>'等待收货', 3=>'已收货'];
		return $arr[$data['status']];
	}
	protected function getDatelineAttr($value, $data){
		return date('Y-m-d H:i', $data['addtime']);
	}
	protected function getAlldataAttr($value, $data){
		$arr = [-1=>'兑换失败', 0=>'未付款', 1=>'等待发货', 2=>'等待收货', 3=>'已收货'];
		$data['realname'] = model('AdminUser')->where('userid', $data['userid'])->value('realname');
		$data['convertname'] = model('Convert')->where('id', $data['convertid'])->value('title');
		$data['statusname'] = $arr[$data['status']];
        $data['dealer'] = model('AdminUser')->get_dealer($data['userid']);
        trace($data['realname']);
		return $data;
	}
	protected function getDealerAttr($value, $data){
        return model('AdminUser')->get_dealer($data['userid']);
    }
	public function save_datas($data){
		$data['ordersn'] = 'convert'.SYS_TIME.rand(1000, 9999);
		$data['nums'] = isset($data['nums']) ? $data['nums'] : 1;
		$data['paytime'] = SYS_TIME;
		$data['paytype'] = 5;
		$data['status'] = 1;
		$convert = model("Convert")->get_data(['item_id'=>$data['convertid']]);
		$data['totalprice'] = $convert['price'] * $data['nums'];
		$data['description'] = '购买 '.$convert['title'];
		$id = model("Order")->save_data($data);
		if (!$id) {
			return false;
		}
		$address = model("MemberAddress")->get_data(['item_id'=>$data['addressid']]);
		$data['name'] = $address['consignee'];
		$data['phone'] = $address['phone'];
		$data['address'] = $address['city'].' '.$address['address'];
		$ids = $this->save_data($data);
		if (!$ids) {
			return false;
		}
		model('Convert')->where('id', $data['convertid'])->setDec('kucun');
		$data['price'] = -$data['totalprice'];
		model('Jifen')->save_data(['userid'=>$data['userid'], 'nums'=>$data['price'], 'typeid'=>7, 'contentid'=>$data['convertid']]);
		// model('Jifen')->data_save(['userid'=>$data['userid'], 'nums'=>$data['price'], 'typeid'=>7, 'contentid'=>$data['convertid']]);
		// model('Jifen')->save_datas($data['userid'], 7, $data['convertid']);
		return model('MemberAmountLog')->add($data, 4, 1, 0, 2);
	}
	public function list_datas($userid){
		$list = $this->list_data(['userid'=>$userid]);
		$lists = [];
		foreach ($list as $v) {
			$nowmonth = date('Y年m月');
			$month = date('Y年m月', $v['addtime']);
			$key = date('Ym', $v['addtime']);
			$lists[$key]['title'] = $nowmonth == $month ? '本月' : $month;
			$lists[$key]['list'][] = $v;
		}
		return $lists;
	}
}	